﻿package com.edward.service;


import java.text.SimpleDateFormat;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.edward.PagerModel;
import com.edward.model.InvoiceList;
import com.edward.model.ShoppingList;

public class ShoppingListManager extends HibernateDaoSupport {

	public void addShoppingList(ShoppingList shoppingList) {
		
		getHibernateTemplate().save(shoppingList);
	}
	
	public ShoppingList getShoppingList(int id){
		ShoppingList s =(ShoppingList)getHibernateTemplate().get(ShoppingList.class, id);
		return s;
	}
	
	@SuppressWarnings("unchecked")
	public ShoppingList getShoppingList(String orderNum){
		List <ShoppingList>list =  getHibernateTemplate().find("from ShoppingList s where s.listNum = '" + orderNum + "'");
			if(list.size()>0){
				return list.get(0);
			}else{
				return null;
			}
	}
	

	@SuppressWarnings("unchecked")
	public List<ShoppingList> listShoppingList(int id) {
		 return getHibernateTemplate().find("from ShoppingList s where invoiceID = " + id);
	}

	
	
	public void updateShoppingList(ShoppingList shoppingList) {
	
		getHibernateTemplate().update(shoppingList);
	}
	
	public void deleteShoppingList(int id){
		
		getHibernateTemplate().delete(getShoppingList(id));
	}
	
	
	@SuppressWarnings("unchecked")
	public PagerModel pagerSL(int start,int limit,int id) {
		int total = getHibernateTemplate().find("from ShoppingList s where invoiceID = " + id).size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <ShoppingList>datas = getHibernateTemplate().find("from ShoppingList s where invoiceID = " + id).subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	
	@SuppressWarnings("unchecked")
	public PagerModel pagerSL(int start,int limit,ShoppingList s) {
		int count = 0;
		   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String sql = "from ShoppingList s where ";
			
			if(!s.getCode().equals("")){
				sql += "s.code = '" + s.getCode() + "'";
				count++;
			}
			if(!s.getListNum().equals("")){
				if(count>0){
					sql +=" and ";
				}
				count++;
				sql += "s.listNum = '" + s.getListNum() + "'";
			}
			if(!s.getState().equals("")){
				if(count>0){
					sql +=" and ";
				}
				count++;
				sql += "s.state = '" + s.getState() + "'";
			}
			if(s.getDate() != null && s.getEndDate() == null ){
				if(count>0){
					sql +=" and ";
				}
				count++;
				String d = sdf.format(s.getDate());
				sql += "s.date >= '" + d + "'";
			}
			if(s.getEndDate() != null && s.getDate() == null ){
				if(count>0){
					sql +=" and ";
				}
				count++;
				String d = sdf.format(s.getEndDate());
				sql += "s.date <= '" + d + "'";
			}
			if(s.getEndDate() != null && s.getDate() != null ){
				if(count>0){
					sql +=" and ";
				}
				count++;
				String d1 = sdf.format(s.getDate());
				String d2 = sdf.format(s.getEndDate());
				sql += "s.date between '" + d1 + "' and '" +d2  +"'";
			}
		   
		int total = getHibernateTemplate().find(sql).size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <ShoppingList>datas = getHibernateTemplate().find(sql).subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	
	@SuppressWarnings("unchecked")
	public PagerModel pagerSL(int start,int limit) {
		int total = getHibernateTemplate().find("from ShoppingList").size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <ShoppingList>datas = getHibernateTemplate().find("from ShoppingList").subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	@SuppressWarnings("unchecked")
	public InvoiceList getInvoice(String code){
	List<InvoiceList>  list = getHibernateTemplate().find("from InvoiceList i where i.invoiceNum = '" + code + "'");
	return list.get(0);
	}
	
	public InvoiceList getInvoice(int id){
		InvoiceList i = (InvoiceList) getHibernateTemplate().get(InvoiceList.class, id);
		return i;
		}
	
	
	//自动添加对应表格的单号
	@SuppressWarnings("unchecked")	
	public String listNum(){
		List <ShoppingList>list = getHibernateTemplate().find("from ShoppingList");
		int i = list.size();
		if(i==0){
			return "000001";
		}else{
			String tmp = list.get(i-1).getListNum();
			while(tmp.indexOf(0) == '0'){
				tmp = tmp.substring(1);
			}
			int t = Integer.parseInt(tmp) + 1;
			tmp = t + "";
			for(int j=6;j>tmp.length();){
				tmp ="0"+tmp;
			}
			return tmp;
		}
		
		
	}
	


}



